<template>
  <ContentWrap class="chem-record-wrap">
    <div class="header-container-consumable">
      <h2 class="centered-title-equipment">{{ form.item }}</h2>
    </div>
    <!-- 头部信息表单 -->
    <el-form :model="form" class="chem-form" label-width="110px" style="margin-bottom: 18px">
      <el-row :gutter="24">
        <el-col :span="4">
          <el-form-item label="检测项目">
            <el-input v-model="form.item" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="样品来源">
            <el-input v-model="form.sampleSource" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="检测类别">
            <el-input v-model="form.testType" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="样品名称">
            <el-input v-model="form.collectorType" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="样品数量">
            <el-input v-model="form.sampleCount" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="检测依据">
            <el-input v-model="form.testStandard" placeholder="" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="4">
          <el-form-item label="接样日期">
            <el-input v-model="form.sampleReceivingTime" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="样品性状">
            <el-input v-model="form.sampleProperty" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="检验日期">
            <el-date-picker
              v-model="form.testDate"
              placeholder="选择日期"
              style="width: 100%"
              type="date"
              value-format="YYYY年MM月DD日"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="采样日期">
            <el-input
              v-model="form.sampleDate"
              :title="form.sampleDate"
              class="overflow-input"
              placeholder=""
            />
          </el-form-item>
        </el-col>

        <el-col :span="4">
          <el-form-item label="仪器信息">
            <el-input v-model="form.instrumentInfo" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="采样依据">
            <el-input v-model="form.sampleStandard" placeholder="" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="4">
          <el-form-item label="检测人员">
            <el-input v-model="form.tester" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="采样人员">
            <el-input v-model="form.sampler" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="定量下限">
            <el-input v-model="form.dlxx" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最低定量浓度">
            <el-input v-model="form.zddlld" placeholder="" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 表格 -->
    <el-table :data="form.table" border class="chem-table" stripe table-layout="fixed">
      <el-table-column align="center" label="样品编号" prop="sampleNo">
        <template #default="{ row }">
          <el-input v-model="row.sampleNo" placeholder="" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="工作场所" prop="workplace">
        <template #default="{ row }">
          <el-input v-model="row.workplace" placeholder="" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="工种/岗位" prop="detectionPost">
        <template #default="{ row }">
          <el-input v-model="row.detectionPost" placeholder="" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="采样点/采样对象及时机" prop="samplePoint">
        <template #default="{ row }">
          <el-input v-model="row.samplePoint" placeholder="" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="采样时段" width="180">
        <template #default="{ row }">
          <el-time-picker
            v-model="row.timeRange"
            end-placeholder="结束时间"
            format="HH:mm"
            is-range
            range-separator="至"
            start-placeholder="开始时间"
            style="width: auto"
            value-format="HH:mm"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="检测结果(mg/m³)" prop="result">
        <template #default="{ row }">
          <el-input v-model="row.result" placeholder="" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注" prop="remarks">
        <template #default="{ row }">
          <el-input v-model="row.remarks" placeholder="" />
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>
</template>

<script lang="ts" setup>
import { onMounted, ref, watch } from 'vue'
import ContentWrap from '@/components/ContentWrap/src/ContentWrap.vue'
import { ProjectApi } from '@/api/dk/project'
import { ElMessage } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import { mergeWith } from 'lodash-es'

const route = useRoute()
const router = useRouter()
const form = ref({
  projectId: route.query.projectId,
  item: '',
  sampleSource: '现场采样',
  testType: '定期检测',
  collectorType: '',
  sampleCount: '', // 这里保持不变，后面用watch自动同步
  sampleReceivingTime: '',
  sampleProperty: '样品完好，无破损',
  tester: '',
  sampler: '',
  instrumentNo: '',
  sampleStandard: 'GBZ 159-2004',
  zddlld: '',
  instrumentInfo: '',
  testStandard: '',
  testDate: '',
  humidity: '',
  sampleDate: '',
  dlxx: '',
  table: [
    {
      sampleNo: '',
      workplace: '',
      detectionPost: '',
      samplePoint: '',
      timeRange: ['', ''],
      result: '',
      remarks: ''
    }
  ]
})
const loading = ref(false)

const props = defineProps<{
  formData: []
  testType: number
}>()

// 自定义合并策略
function customizer(objValue, srcValue) {
  // 如果源值是空值(null, undefined, '')，则保留目标值
  if (srcValue === null || srcValue === undefined || srcValue === '') {
    return objValue
  }
  // 对于数组，特殊处理
  if (Array.isArray(srcValue)) {
    return srcValue.length > 0 ? srcValue : objValue
  }
}

watch(
  form,
  (newValue, oldValue) => {
    console.log('form changed:', newValue)
    // 在这里执行你的逻辑

    if (!loading.value) {
      loading.value = false
    }
  },
  { deep: true } // 深度监听，因为 form 是嵌套对象
)

// 监听table长度变化，自动同步sampleCount
watch(
  () => props.formData,
  (newDustData) => {
    console.log('从父类获取数据:', JSON.stringify(newDustData))
    form.value = mergeWith({ ...form.value }, newDustData, customizer)
    form.value.sampleCount = newDustData.table.length
  },
  {
    immediate: true
  }
)

onMounted(async () => {
  loading.value = true
  const projectId = route.query.projectId as string
  const { sampler: samplerData } = await ProjectApi.getProject(Number(projectId))
  // console.log('数据', JSON.stringify(data))
  form.value.sampler = samplerData
})

</script>

<style lang="scss" scoped>

:deep(.el-input__inner) {
  text-align: center;
}

.overflow-input input {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.header-container-consumable {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid #e5e6eb;
}

.centered-title-equipment {
  text-align: center; // Center the text
  flex-grow: 1; // Allow title to take available space to help centering
  margin: 0; // Remove default margin
  font-size: 24px; // Match Consumable title size
  font-weight: bold;
}

.chem-form {
  background: none;
  box-shadow: none;
  border-radius: 0;
  margin-bottom: 0;

  :deep(.el-input__inner) {
    border-radius: 0 !important;
    box-shadow: none !important;
  }

  :deep(.el-form-item__label) {
    white-space: nowrap;
  }

  :deep(.el-input__inner) {
    text-align: left;
  }
}

.chem-btns {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  align-items: center;
  margin-top: 0;
}

.chem-table {
  margin-top: 18px;
  --el-table-border-radius: 0 !important;
  --el-table-header-bg-color: #f7f8fa;
  --el-table-border-color: #e5e6eb;
  box-shadow: none !important;
  border-radius: 0 !important;

  :deep(.el-table__header th) {
    background: #f7f8fa !important;
    font-weight: 600;
    font-size: 14px;
    border-radius: 0 !important;
    padding: 8px 0 !important;

    .cell {
      padding-left: 0px !important;
      padding-right: 0px !important;
    }
  }

  // 设置表格行高
  :deep(.el-table__body td) {
    border-radius: 0 !important;

    font-size: 14px;
    padding: 0 !important;
    // 列和单元格的边距
    .cell {
      padding: 0px 0px;
    }
  }

  // 去掉输入框边框
  :deep(.el-input__wrapper) {
    border-radius: 0 !important;
    box-shadow: none !important;
  }

  // 针对下拉选择框的所有相关元素去掉边框
  :deep(.el-select__wrapper) {
    border: none !important;
    box-shadow: none !important;
  }
}

.input-with-unit {
  position: relative;
}



.unit-text {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #909399;
  font-size: 14px;
  pointer-events: none;
  z-index: 1;
}
</style>
